package br.com.hstar.client.produto.centrocusto;

import br.com.hstar.client.ServiceFactory;
import br.com.hstar.client.util.FormPanel;
import br.com.hstar.client.util.HstarDialogBox;
import br.com.hstar.client.util.HstarUserException;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class CentroCustoEditPanel extends FormPanel {

	private Button incluir;
	private Button cancelar;
	private Button excluir;
	
	private TextBox nome;
	private CentroCustoUI centroCustoUI;

	public CentroCustoEditPanel(String _title) {
		super(_title);
		centroCustoUI = new CentroCustoUI();
		init(true);
	}
	
	public CentroCustoEditPanel(String _title, Long id) {
		super(_title);
		centroCustoUI = new CentroCustoUI();
		centroCustoUI.setId(id);
		init(false);
		ServiceFactory.createCentroCustoService().retrieveById(id, new RetrieveAsyncCallback());
	}
	
	
	
	//Methods -------------------------------------------------------------------
	
	private void init(boolean isNew) {
		
		nome 		= addTextBox("Nome", 30, 30, "");
		
		if(isNew) {
			incluir  = addButton("Incluir");
			cancelar = addButton("Cancelar");
		} else {
			incluir  = addButton("Alterar");
			cancelar = addButton("Cancelar");
			excluir = addButton("Excluir");
			excluir.addClickListener(new ClickListener() {
				public void onClick(Widget sender) {
					ServiceFactory.createCentroCustoService().delete(centroCustoUI.getId(), new DeleteAsyncCallback());
				}
			});
		}
		
		incluir.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				 try {
					copyData();
					ServiceFactory.createCentroCustoService().saveOrUpdate(centroCustoUI, new SaveAsyncCallback());
				} catch (HstarUserException e) {
					new HstarDialogBox("Centro de Custo", e.getMessage());
				}
				 
			}
		});
		cancelar.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				RootPanel.get("boxContent").clear();
				RootPanel.get("boxContent").add(new CentroCustoListPanel("Centro de Custo"));
			}
		});
		
	}

	private void copyData() throws HstarUserException {
		
		if(nome.getText().trim() == "")
		{			
			throw new HstarUserException(" - Digite o nome do centro de custo. <br>");
		}
		
		
		
		centroCustoUI.setNome(nome.getText().trim());
	}

	// INNER CLASSES ----------------------------------------------------------------
	private class SaveAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			if(caught instanceof HstarUserException)
			{					
				new HstarDialogBox("Centro de custo",caught.getMessage());
			}
		}

		public void onSuccess(Object result) {
			RootPanel.get("boxContent").clear();
			RootPanel.get("boxContent").add(new CentroCustoListPanel("Centro de Custo"));
		}
	}
	
	private class DeleteAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("ERRO - CentroCusto edit panel");
		}

		public void onSuccess(Object result) {
			RootPanel.get("boxContent").clear();
			RootPanel.get("boxContent").add(new CentroCustoListPanel("Centro de Custo"));
		}
	}
	
	//carrega os campos. Usado para quando está editando
	private class RetrieveAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			Window.alert("ERRO - Status do Andamento edit panel");
		}

		public void onSuccess(Object result) {
			CentroCustoUI centroCusto = (CentroCustoUI)result;

			nome.setText(centroCusto.getNome().toString().trim());
			
		}
	}
}      